<template>
	<view>
		<view class="bc_img">
			<image :src=" imgurl+ paybg" style="width: 750rpx;height: 1556rpx;"></image>
		</view>
		<view class="bc-qp">
			<pinazi-game-chinese-chess ref="chess" @fail="failfun" @succe="succefun"></pinazi-game-chinese-chess>
		</view>


		<!-- 功能按钮 -->
		<view class="goBang-btns">
			<!-- <view class="goBang-btn" @click="regret" v-if="!isOver">
						<u-icon name="thumb-down-fill" size="30" color="#999"></u-icon>
						<text>悔棋</text>
					</view> -->
			<view class="goBang-btn" @click="restart">
				<u-icon name="reload" size="30" color="#999"></u-icon>
				<text>重来</text>
			</view>
			<view class="goBang-btn" @click="defeat">
				<u-icon name="fingerprint" size="30" color="#999"></u-icon>
				<text>认输</text>
			</view>
		</view>
		<!-- 规则说明 -->
		<!-- <view class="boxtb">
					<view class="u-m-t-10 fontBold u-font-32 c757575">规则说明:</view>
					<view class="u-m-t-20 c757575">1、玩家先手</view>
					<view class="u-m-t-10 c757575">2、其他规则，不知道就百度去，惯得！！！</view>
				</view> -->


		<u-popup v-model="gameshow" mode="center" border-radius="35" @close="gameclose" :closeable="true"
			:overlay="true">
			<view class="fail-k">
				<view class="fail-title">
					活动结束感谢您的参与
				</view>
				<view class="fail-content">
					积分已到账，请注意查收
				</view>
				<view class="fail-bt" @click="goshop">
					进入商城
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		finishActivity
	} from "@/api/index/index";
	import {
		BASE_URL_IMAHE
	} from "../../../utils/https";
	const app = getApp()
	export default {
		data() {
			return {
				imgurl: BASE_URL_IMAHE,
				title: 'name',
				gameshow: false,
				jumpShopLink: "",
				form: {
					activityId: "",
					whiteListId: "",
					joinRecordId: "",
					userRef: "",
					token: "",
				},
				paybg: "",
			}
		},
		onLoad(options) {
			this.form = {
				...options
			}
			var sjs = this.randomWord()
			var datatime = Date.parse(new Date());
			var tokens = '' + sjs + '' + datatime
			this.form.token = tokens

			this.paybg = app.globalData.data.inpayimg

			if (!uni.getStorageSync("userRef")) {
				uni.reLaunch({
					url: "/pages/home/index"
				})
			} else {
				this.form.userRef = uni.getStorageSync("userRef")
			}
			if (!options.activityId || !options.whiteListId || !options.joinRecordId) {
				// 跳转回原页面
				if (getCurrentPages().length == 1) {
					uni.reLaunch({
						url: "/pages/home/index"
					})
				} else {
					uni.navigateBack({
						delta: 1,
					});
				}

			} else {
				this.form.activityId = options.activityId
				uni.showLoading({
					title: "加载中",
				})
			}
			// this.chess_init();
			uni.showToast({
				title: "欢迎来到中国象棋",
				icon: 'none'
			});

		},
		methods: {
			goshop(url) {
				window.location.href = this.jumpShopLink
			},
			gameclose() {
				uni.reLaunch({
					url: "/pages/home/index"
				})
			},
			//提交成績
			async finishActivityfun() {
				const res = await finishActivity(JSON.stringify(this.form))
				if (res.result == 0) {
					this.gameshow = true
					this.jumpShopLink = res.jumpShopLink
				} else {
					uni.showToast({
						icon: "error",
						title: res.message,
					})
				}

			},

			restart() {
				console.log(this.$refs.chess)
				this.$refs.chess.init()
			},

			failfun() {
				uni.showToast({
					title: "您已失败",
					icon: "none"
				})
				setTimeout(() => {
					this.finishActivityfun()
				}, 1000)
			},
			// 认输
			defeat() {


				uni.showToast({
					title: "您已认输",
					icon: 'none'
				});
				setTimeout(() => {
					this.finishActivityfun()
				}, 1000)
			},
			succefun() {
				uni.showToast({
					title: "您已获胜",
					icon: "none"
				})
				setTimeout(() => {
					this.finishActivityfun()
				}, 1000)
			},
			randomWord() {
				var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
					'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
					'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
					'x', 'y', 'z'
				];
				var nums = "";
				for (var i = 0; i < 20; i++) {
					var id = parseInt(Math.random() * 61);
					nums += chars[id]
				}
				return nums;
			},

		}
	}
</script>

<style scoped lang="scss">
	.bc-qp {
		z-index: 9;
		position: relative;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;

	}

	.bc_img {
		position: absolute;
		top: 0;
		left: 0;
		width: 750rpx;
		height: 100vh;
		display: flex;
		justify-content: center;
		overflow: hidden;
		z-index: 2;

		/* image{
			width: 750rpx;
			height: 1556rpx;
		} */
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.goBang-btns {
		position: relative;
		z-index: 9;
		display: flex;
		align-items: center;
		justify-content: center;
		/* position: fixed; */
		/* bottom: 10rpx;
		right: 0;
		left: 0; */
		padding: 30rpx;

		/* position: absolute; */
		.goBang-btn {
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
			background-color: #fff;
			box-shadow: 0 0 10rpx 4rpx rgba(0, 0, 0, .1);
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			margin-left: 30rpx;
			color: #999;
			font-size: 24rpx;
		}
	}

	.fail-k {
		width: 490rpx;
		height: 405rpx;
		text-align: center;

		.fail-title {
			padding-top: 120rpx;
			font-size: 36rpx;
			font-weight: bold;
			color: #111827;
		}

		.fail-content {
			margin-top: 29rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #718096;
		}

		.fail-bt {
			margin: 55rpx auto 0rpx;
			width: 328rpx;
			height: 64rpx;
			background-color: #292c34;
			color: #ffffff;
			border-radius: 35rpx;
			line-height: 64rpx;

		}

	}
</style>